PLUGIN.Name = "Spawn Icon Generation Progress Bar"
PLUGIN.DefaultOn = true
PLUGIN.Description = "Replaces the chat entries created by the creation of a spawn icon with a progress bar on the bottom of the screen"
PLUGIN.Author = "Kogitsune"

function PLUGIN:Load( )
	self.cBarColor = Color( 000, 189, 000, 150 )
	self.cTextColor = Color( 255, 255, 255, 255 )
	self.cBackground = Color( 000, 000, 000, 150 )
	
	self.Font = "ScoreboardText"
	self.Number = 0
	self.Maximum = 0
	
	self.TextFormat = "Generating spawn icons ( %d of %d remain )"
	
	self.Y = ScrH( ) + 32
	self.W = 0
	
	self.bHidden = true
	
end

function PLUGIN:LoadParameters( )
end

function PLUGIN:Unload( )
end

function PLUGIN.HOOK:HUDPaint( )
	if self.bHidden and self.Number == 0 then return end

	local r, n, s, x
	
	surface.SetFont( self.Font )
	s = self.TextFormat:format( self.Number, self.Maximum + 1 )
	
	r, n = surface.GetTextSize( s )
	
	self.Y = math.Approach( self.Y, self.Number == 0 and ScrH( ) + n or ScrH( ) - n, RealFrameTime( ) * n * 3 )
	self.W = math.Approach( self.W, ( self.Maximum - self.Number ) / self.Maximum, RealFrameTime( ) * ( self.Maximum - self.Number ) / self.Maximum * 4 )
	
	x = ScrW( ) * .5	
	
	surface.SetDrawColor( self.cBackground.r, self.cBackground.g, self.cBackground.b, self.cBackground.a )
	surface.DrawRect( 0, self.Y, ScrW( ), n )
	
	surface.SetDrawColor( self.cBarColor.r, self.cBarColor.g, self.cBarColor.b, self.cBarColor.a )
	surface.DrawRect( 0, self.Y, ScrW( ) * self.W, n )
	
	draw.DrawText( s, self.Font, x, self.Y, self.cTextColor, TEXT_ALIGN_CENTER )
	
	if self.Y > ScrH( ) and self.Number == 0 then
		self.Number = 0
		self.Maximum = 0
		self.W = 0
		self.bHidden = true
		
	else
		self.bHidden = false
		
	end
	
end

function PLUGIN.HOOK:ChatText( pindex, pname, text, mess )
	if mess == "none" then
		local n
		
		n = text:match( "^Generated Spawn Icon %((%d+) left in queue%)$" )
		
		if n then
			self.Number = tonumber( n ) or 0
			self.Maximum = math.max( n, self.Maximum )
			return true
			
		end
		
	end
	
end
